package IP;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            // 获取本机IP地址
            InetAddress inetAddress =  InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress);

            inetAddress =  InetAddress.getByName("localhost");
            System.out.println(inetAddress);

            inetAddress =  InetAddress.getLocalHost();
            System.out.println(inetAddress);

            // 获取域名IP地址
            inetAddress =  InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress);

            // 常用方法 —— 针对百度
            // System.out.println(inetAddress.getAddress());
            System.out.println(inetAddress.getCanonicalHostName()); // 规范的名字
            System.out.println(inetAddress.getHostAddress()); // IP
            System.out.println(inetAddress.getHostName()); // 域名
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
